package com.yd.mq.parse.utils;

import java.io.File;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 并发入库
 * 
 * @ClassName AlarmParseUtils
 * @author z
 * @date 2017年9月8日 下午5:13:24
 */
@Component
public class AlarmParseUtils {

	/* 日志处理 */
	private Logger	log	= Logger.getLogger(this.getClass().getName());

	/* 配置数据所在的路径 */
	private static final String	DATA_ELEMENT = "FM_ALARM_MSG";

	@Autowired
	private Alarm2DBService	service;

	private ExecutorService executorService	= Executors.newCachedThreadPool();

	/**
	 * 实时入库 @return void @throws
	 */

	public void init(String msgXml) {
		executorService.execute(new Runnable() {
			@Override
			public void run() {
				try {
					Map<String, String> map = msgParse(msgXml);
					// 执行解析入库
					service.init(map, null);
				} catch (DocumentException e) {
					e.printStackTrace();
					log.info("解析文件异常！" + e.getMessage());
				}
			}
		});
	}

	/**
	 * 入库操作 @return void @throws
	 */
	public void init(File msgXml, File file) {

		try {
			Map<String, String> map = msgParse(msgXml);
			// 执行解析入库

			/*
			 * final String alarmNo = map.get("ProbableCause");// 告警码 final
			 * String ActiveStatus = map.get("ActiveStatus"); if
			 * (alarmNo.equals("21807")) {
			 * log.info("21807==============================" + path);
			 * 
			 * } if (alarmNo.equals("21807") && ActiveStatus.equals("0")) {
			 * log.info("ActiveStatus 0===========================" + path);
			 * 
			 * }
			 */
			service.init(map, file);
		} catch (DocumentException e) {
			e.printStackTrace();
			log.info("解析文件异常！" + e.getMessage());
		}

	}

	/**
	 * 解析xml @return Map<String,String> @throws
	 */
	public Map<String, String> msgParse(String msgXml) throws DocumentException {
		// 加载当前xml
		Document document = DocumentHelper.parseText(msgXml);

		// 获取根节点下的指定节点-WholeMsg/FM_ALARM_MSG
		Element dataElement = document.getRootElement().element(DATA_ELEMENT);
		List<Element> skills = dataElement.elements();
		Map<String, String> map = new HashMap<>();
		for (Iterator<?> it = skills.iterator(); it.hasNext();) {
			Element element = (Element) it.next();
			final String name = element.getName();
			final String text = element.getText();
			map.put(name, text);
		}

		return map;
	}

	/**
	 * 解析xml @return Map<String,String> @throws
	 */
	public Map<String, String> msgParse(File file) throws DocumentException {
		// 加载当前xml
		SAXReader sax = new SAXReader();
		sax.setEncoding("UTF-8");
		Document document = sax.read(file);
		// 获取根节点下的指定节点-WholeMsg/FM_ALARM_MSG
		Element dataElement = document.getRootElement().element(DATA_ELEMENT);

		@SuppressWarnings("rawtypes")
		List skills = dataElement.elements();
		Map<String, String> map = new HashMap<>();
		for (Iterator<?> it = skills.iterator(); it.hasNext();) {
			Element element = (Element) it.next();
			final String name = element.getName();
			final String text = element.getText();
			map.put(name, text);
		}

		return map;
	}

	@SuppressWarnings("deprecation")
	public static void main(String[] args) throws ParseException {
		AlarmParseUtils fileTest = new AlarmParseUtils();
		Date date = new Date();
		date.setHours(date.getHours() - 1);

		String pathstr = null;
		pathstr = "F:\\IDEA\\MQ\\2017\\09\\08\\00\\20170906000007_14838f779b324beca7a34a8c6cb70bce.xml";

		// 4g
		pathstr = "F:\\IDEA\\MQ\\2017\\09\\08\\00\\20170906000253_415857ec96cf4c7f895df698ced9103a.xml";

		File fileXml = new File(pathstr);

		try {
			Map<String, String> map = fileTest.msgParse(fileXml);

			String alarmText = map.get("AlarmText");// 报文原始信息
			System.out.println(alarmText);
			// String subInfo = getSubInfo2G(alarmText);
			// String subInfo = getSubInfo4G(alarmText);

		} catch (DocumentException e) {
			e.printStackTrace();
		}

	}

}